<template>
  <el-dialog v-if="dialogVisible" title="默认值设置" :visible.sync="dialogVisible" width="50%">
    <el-form inline label-position="top" style="overflow: scroll">
      <el-form-item>
        <template #label style="display: inline-block">
          <span v-html="copyCurrentItem.config.label" />
        </template>
        <component
          :is="copyCurrentItem.config.tag"
          v-model="copyCurrentItem.config.defaultValue"
          class="form-item-component"
          v-bind="{ ...copyCurrentItem }"
          :item="copyCurrentItem"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="setDefaultValue">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import draggable from 'vuedraggable'
import FormItem from './FormItem'
import { jsonSimpleClone } from '../../utils'

export default {
  name: 'DefaultValueConfig',
  components: {
    FormItem,
    draggable
  },
  props: ['currentItem', 'index'],
  data() {
    return {
      copyCurrentItem: {},
      dialogVisible: false
    }
  },
  watch: {
    currentItem: {
      deep: true,
      handler(val) {
        this.copyItem()
      }
    }
  },
  created() {
    this.copyItem()
  },
  methods: {
    copyItem() {
      let item = jsonSimpleClone(this.currentItem)
      item.readonly = false
      this.copyCurrentItem = item
    },
    open() {
      this.dialogVisible = true
    },
    setDefaultValue() {
      this.$set(this.currentItem.config, 'defaultValue', this.copyCurrentItem.config.defaultValue)
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped></style>
